package appapi.controller.oider;

import appapi.Request.DeleteStaffroleReq;
import appapi.Request.StaffrolePageReq;
import appapi.Request.StaffroleReq;
import appapi.Vo.AddStaffRoleVo;
import appapi.common.ResponseBean;
import appapi.dto.StaffroleDto;
import appapi.entity.auth.StaffroleEntity;
import appapi.service.StaffroleService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户角色
 *
 * @author lc
 * @version 1.0
 * @date 2023-05-17 10:08:27
 */
@Api(tags = "用户角色")
@RestController
@RequestMapping("/api/staffrole")
public class StaffroleController {

    private static final Logger logger = LoggerFactory.getLogger(StaffroleController.class);
    @Autowired
    private StaffroleService staffroleService;

    /**
     * 分页列表
     */
    @GetMapping(value = "/getStaffRoleListByPage")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "formData", name = "staffroleReq", dataType = "object", value = "分页列表")
    })
    @ApiOperation(value = "获取用户角色分页列表")
    public ResponseBean<StaffroleDto> page(StaffrolePageReq staffroleReq) {
        PageInfo<StaffroleDto> pageInfo = staffroleService.page(staffroleReq);
        return ResponseBean.success(pageInfo);
    }

    /**
     * 分页列表
     */
    @GetMapping("/getStaffRoleList")
    @ApiOperation(value = "获取用户角色列表")
    public ResponseBean<StaffroleDto> list(StaffroleReq staffroleReq) {
        try {
            List<StaffroleDto> list = staffroleService.list(staffroleReq);
            return ResponseBean.success(list);
        } catch (Exception e) {
            logger.error("用户角色分页列表出错", e);
            return ResponseBean.fail(e.getMessage());
        }
    }

    /**
     * 信息
     */
    @GetMapping("/getStaffRole")
    @ApiOperation(value = "用户角色信息")
    public ResponseBean<StaffroleEntity> detail(Long staffId) {
        try {
            if (staffId == null) {
                return ResponseBean.fail("用户ID不能为空");
            }
            List<StaffroleEntity> staffroleEntity = staffroleService.detail(staffId);
            return ResponseBean.success(staffroleEntity);
        } catch (Exception e) {
            logger.error("用户角色信息出错", e);
            return ResponseBean.fail(e.getMessage());
        }
    }

    /**
     * 新增
     */
    @PostMapping(value = "/addStaffrole")
    @ApiOperation(value = "新增用户角色")
    public ResponseBean insert(@RequestBody AddStaffRoleVo staffRoleVo) {
        try {
            if(staffRoleVo.getRoleIds().size()==0)
            {
                return ResponseBean.fail("请选择角色");
            }
            //获取用户现有角色
            Long staffId =staffRoleVo.getStaffId();
            List<StaffroleEntity> oldList =staffroleService.detail(staffId);
            List<StaffroleEntity> addList = new ArrayList<>();

            //判断用户新增角色
            for (Long roleId:staffRoleVo.getRoleIds()
            ) {
                //新增
                if(oldList.stream().filter(StaffroleEntity -> StaffroleEntity.getRoleId().equals(roleId)).count()==0) {
                    StaffroleEntity entity = new StaffroleEntity();
                    entity.setRoleId(roleId);
                    entity.setStaffId(staffId);
                    entity.setStaffRoleNote(staffRoleVo.getNote());
                    entity.setCreateTime(LocalDateTime.now());
                    entity.setUpdateTime(LocalDateTime.now());
                    addList.add(entity);
                }
            }
            //删除删除原有角色
            List<Long> delList =new ArrayList<>();
            for (StaffroleEntity oldAuth:oldList
            ) {
                if(staffRoleVo.getRoleIds().stream().filter(roleId -> roleId.equals(oldAuth.getRoleId())).count()==0) {
                    delList.add(oldAuth.getRoleId());
                }
            }
            if(delList.size()>0) {
                staffroleService.deleteBatch(staffId,delList);
            }
            if(addList.size()>0) {
                staffroleService.insertBatch(addList);
            }
            return ResponseBean.success("添加角色成功");

        } catch (Exception ex) {
            return ResponseBean.fail(ex.getMessage());
        }
    }


    /**
     * 删除
     */
    @PostMapping(value = "/deleteStaffrole")
    @ApiOperation(value = "删除用户角色")
    public ResponseBean delete(@RequestBody DeleteStaffroleReq req) {
        try {
            if (req.getStaffRoleIds() == null) {
                return ResponseBean.fail("记录ID不能为空");
            }
            if (req.getStaffRoleIds().size()==0) {
                return ResponseBean.fail("记录ID不能为空");
            }
            int iRet = staffroleService.delete(req.getStaffRoleIds());
            if (iRet > 0) {
                return ResponseBean.success("删除成功");
            } else {
                return ResponseBean.fail("删除失败");
            }
        } catch (Exception ex) {
            return ResponseBean.fail(ex.getMessage());
        }
    }

}
